package com.zlk.algorithm.huawei.leetcode.matrix;

import jdk.nashorn.internal.runtime.regexp.JoniRegExp;
import org.junit.Test;

import java.util.Arrays;

/**
 * @program: algorithm
 * @ClassName Code11_setZeroes
 * @description:
 * @author: slfang
 * @create: 2025-01-03 17:50
 * @Version 1.0
 **/
public class Code11_setZeroes {



    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean flagCol0 = false;
        for (int i = 0; i < m; i++) {
            if (matrix[i][0] == 0) {
                flagCol0 = true;
            }
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = matrix[0][j] = 0;
                }
            }
        }


        for (int i = m - 1; i >= 0; i--) {
            for (int j = 1; j < n; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
            if (flagCol0) {
                matrix[i][0] = 0;
            }
        }
    }

    //暴力方法
    public void setZeroes1(int[][] matrix) {
        int n = matrix.length;
        int m = matrix[0].length;
        int[] temp = new int[m];
        Arrays.fill(temp,-1);
        int[][] originMatrix = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                originMatrix[i][j]=matrix[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(originMatrix[i][j]==0){
                    for (int ii = 0; ii < n; ii++) {
                        matrix[ii][j] = 0;
                    }
                    for (int jj = 0; jj < m; jj++) {
                        matrix[i][jj]=0;
                    }
                }
            }
        }
        System.out.println(Arrays.toString(matrix));
    }

    @Test
    public void test(){
        int[][] matrix =new int[3][3];
        matrix[0] = new int[]{1,1,1};
        matrix[1] = new int[]{1,0,1};
        matrix[2] = new int[]{1,1,1};
        setZeroes(matrix);
    }
}
